#include "SyncWaiter.h"


bool gee::SyncWaiter::wait( uint32 ms /*= -1*/ )
{
	if(e)
	{
		DWORD res = ::WaitForSingleObject(e,(DWORD)ms);
		
		return (res == WAIT_TIMEOUT) ? false : true;
	}

	return true;
}

gee::SyncWaiter::SyncWaiter( const char* name ):e(NULL)
{
	e = ::CreateEventA(NULL,FALSE,FALSE,name);
}

gee::SyncWaiter::~SyncWaiter()
{
	if(e != NULL)
	{
		::CloseHandle(e);
		e = NULL;
	}
}

void gee::SyncWaiter::post()
{
	if(e != NULL)
		::SetEvent(e);
}

gee::SemaphoreWaiter::SemaphoreWaiter( const char* name /*= NULL*/,uint32 maxWaitCount /*= 8*/ )
{
	e = ::CreateSemaphoreA(NULL,0,(LONG)maxWaitCount,name);
	assert(e);
}

gee::SemaphoreWaiter::~SemaphoreWaiter()
{
	if(e!=NULL)
	{
		::CloseHandle(e);
		e = NULL;
	}

}

void gee::SemaphoreWaiter::wait( uint32 ms /*= -1*/ )
{
	if(e)
		WaitForSingleObject(e,(DWORD)ms);
}

void gee::SemaphoreWaiter::post(uint32 count/*=1*/)
{
	if(e)
		::ReleaseSemaphore(e,(LONG)count,NULL);
}